﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BackupLibrary;
using MasterLibrary.Windows.Forms.Aero;

namespace Prometheus
{
    public partial class wzSettingsDataExclude : WizardPage
    {
        public BackupContainer backupContainer { get; set; }

        public event EventHandler<wzSettingsDataExcludeChangedEventArgs> ContainerChanged;
        private void OnContainerChanged(wzSettingsDataExcludeChangedEventArgs e)
        {
            EventHandler<wzSettingsDataExcludeChangedEventArgs> ev = ContainerChanged;
            if (ev != null) ev(this, e);
        }

        public wzSettingsDataExclude(BackupContainer c)
        {
            InitializeComponent();

            this.backupContainer = c;
            this.ReloadBackupCollection();
        }

        public void ReloadBackupCollection()
        {
            this.SuspendLayout();
            this.listView.SuspendLayout();

            this.listView.Clear();

            this.listView.Columns.Add("Quellpfad", 480, HorizontalAlignment.Left);

            foreach (string val in this.backupContainer.ExcludedItems)
            {
                if (File.Exists(val))
                {
                    this.listView.Items.Add(new ListViewItem(new string[] { val }, 3));
                    continue;
                }
                if (Directory.Exists(val))
                {
                    this.listView.Items.Add(new ListViewItem(new string[] { val }, 0));
                    continue;
                }

                this.listView.Items.Add(new ListViewItem(new string[] { val }, 6));
            }

            this.listView.ResumeLayout();
            this.ResumeLayout();
        }

        private void btnAddFile_Click(object sender, EventArgs e)
        {
            this.openFileDialog.RestoreDirectory = true;
            DialogResult result = this.openFileDialog.ShowDialog(Host);

            if (result == DialogResult.OK)
            {
                this.backupContainer.ExcludedItems.Add(this.openFileDialog.FileName);
                
                this.ReloadBackupCollection();
                Host.Activate();

                this.OnContainerChanged(new wzSettingsDataExcludeChangedEventArgs(this.backupContainer));
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog.ShowNewFolderButton = false;
            DialogResult result = this.folderBrowserDialog.ShowDialog(Host);

            if (result == DialogResult.OK)
            {
                this.backupContainer.ExcludedItems.Add(this.folderBrowserDialog.SelectedPath);

                this.ReloadBackupCollection();
                Host.Activate();

                this.OnContainerChanged(new wzSettingsDataExcludeChangedEventArgs(this.backupContainer));
            }
        }

        private void löschenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.btnDelete_Click(sender, e);
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //if (this.listView.SelectedItems.Count == 1)
            //{
                this.backupContainer.ExcludedItems.RemoveAt(this.listView.SelectedItems[0].Index);

                this.ReloadBackupCollection();
                this.OnContainerChanged(new wzSettingsDataExcludeChangedEventArgs(this.backupContainer));
            //}
        }
    }

    #region EventArgs
    public class wzSettingsDataExcludeChangedEventArgs : EventArgs
    {
        public BackupContainer Container { get; set; }

        public wzSettingsDataExcludeChangedEventArgs(BackupContainer Container)
        {
            this.Container = Container;
        }
    }
    #endregion
}
